package secondtest.Impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import secondtest.dao.UserDao;
import secondtest.po.User;

import java.util.List;

/**
 * 这是原始的Dao实现方式
 */
public class UserImpl implements UserDao {
    private SqlSessionFactory sessionFactory;

    public UserImpl(SqlSessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public User findUserByid(int id) {
        SqlSession sqlSession = sessionFactory.openSession();
        return sqlSession.selectOne("secondtest.mapper.UserMapper.findUserByid", 1);
    }

    @Override
    public List<User> findUserByName(String name) {
        SqlSession sqlSession = sessionFactory.openSession();
        return sqlSession.selectList("secondtest.mapper.UserMapper.findUserByName", "小明");
    }

    @Override
    public void insertUser(User user) {
        SqlSession sqlSession = sessionFactory.openSession();
        sqlSession.insert("secondtest.mapper.UserMapper.insertUser", user);
        sqlSession.commit();
    }

    @Override
    public void deleteUser(int id) {
        SqlSession sqlSession = sessionFactory.openSession();
        sqlSession.delete("secondtest.mapper.UserMapper.deleteUser", id);
        sqlSession.commit();
    }

    @Override
    public void updateUser(User user) {
        SqlSession sqlSession = sessionFactory.openSession();
        sqlSession.update("secondtest.mapper.UserMapper.updateUser", user);
        sqlSession.commit();
    }
}
